package com.jiot.controller.timing;

import com.zg.cloud.BaseResponse;
import com.zg.cloud.annotation.TokenCheck;
import com.zg.cloud.service.TimingTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import req.device.SelectDeviceAllReq;
import req.job.*;
import resp.device.SelectDeviceAllResp;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author hushi
 * @date 2020/10/27 17:54
 * @description
 */
@RestController
@RequestMapping("/device/task")
@Api(value = "设备模块")
public class TimingTaskController {

    @Resource
    private TimingTaskService timingTaskService;

    @PostMapping("/select/device/all")
    @TokenCheck
    @ApiOperation(value = "根据设备id查询定时任务", notes = "device")
    public BaseResponse findByDeviceId(@RequestBody @Valid FindByDeviceIdReq findByDeviceIdReq) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setData(timingTaskService.findByDeviceId(findByDeviceIdReq));
        return baseResponse;
    }

    @PostMapping("/add/timing/task")
    @TokenCheck
    @ApiOperation(value = "添加定时任务", notes = "device")
    public BaseResponse addTimingTask(@RequestBody @Valid AddimingTaskReq addTimingTaskReq) {
        timingTaskService.addTimingTask(addTimingTaskReq);
        return new BaseResponse();
    }

    @PostMapping("/modify/timing/task")
    @TokenCheck
    @ApiOperation(value = "修改定时任务", notes = "device")
    public BaseResponse modifyTimingTask(@RequestBody @Valid ModifyTimingTaskReq modifyTimingTaskReq) {
        timingTaskService.modifyTimingTask(modifyTimingTaskReq);
        return new BaseResponse();
    }

    @PostMapping("/del/timing/task")
    @TokenCheck
    @ApiOperation(value = "删除定时任务", notes = "device")
    public BaseResponse delTimingTask(@RequestBody @Valid DelTimingTaskReq delTimingTaskReq) {
        timingTaskService.delTimingTask(delTimingTaskReq);
        return new BaseResponse();
    }

    @PostMapping("/timing/task/switch")
    @TokenCheck
    @ApiOperation(value = "定时任务开关", notes = "device")
    public BaseResponse timingTaskSwitch(@RequestBody @Valid TimingTaskSwitchReq timingTaskSwitchReq) {
        timingTaskService.timingTaskSwitch(timingTaskSwitchReq);
        return new BaseResponse();
    }

    @PostMapping("/all/timing/task/switch")
    @TokenCheck
    @ApiOperation(value = "一键定时任务开关", notes = "device")
    public BaseResponse allTimingTaskSwitch(@RequestBody @Valid AllTimingTaskSwitchReq allTimingTaskSwitchReq) {
        timingTaskService.allTimingTaskSwitch(allTimingTaskSwitchReq);
        return new BaseResponse();
    }

    @PostMapping("/query/the/latest/scheduled/task")
    @TokenCheck
    @ApiOperation(value = "根据用户查询马上执行的定时任务", notes = "device")
    public BaseResponse queryTheLatestScheduledTask() {
        return new BaseResponse(timingTaskService.queryTheLatestScheduledTask());
    }

    @PostMapping("/earliest/task/time")
    @TokenCheck
    @ApiOperation(value = "查询项目任务最快执行时间", notes = "device")
    public BaseResponse earliestTaskTime(@RequestBody @Valid GetProjectEarliestTaskTimeReq getProjectEarliestTaskTimeReq) {
        return new BaseResponse(timingTaskService.earliestTaskTime(getProjectEarliestTaskTimeReq));
    }
}
